var OpenAPI = {
		//"requrl":"http://211.149.238.112:9080/mobile/api"//测试
		//"requrl":"https://itsm.changan.com.cn/mobile/api"//生产

    // "requrl":"http://192.168.191.1:8080/mobile/api",
    // "getCommentUrl": 'http://192.168.191.1:8080/itsm17/workorder/opinion/getByOrderId?orderId=',
    // "saveCommentUrl": 'http://192.168.191.1:8080/itsm17/workorder/opinion/save'

	//"loginUrl": "http://10.64.18.194:8080/itsm-mobile/auth",
	//"updateUrl": "http://10.64.18.194:8080/itsm17/mobile/picture/upload",
    //"requrl":"http://10.64.18.194:8080/mobile/api",
   // "getCommentUrl": 'http://10.64.18.194:8080/itsm17/workorder/opinion/getByOrderId?orderId=',
    //"saveCommentUrl": 'http://10.64.18.194:8080/itsm17/workorder/opinion/save'
		
		"loginUrl": "http://61.186.243.117:9091/itsm-mobile/auth",
		"updateUrl": "http://61.186.243.117:9091/itsm17/mobile/picture/upload",
	    "requrl":"http://61.186.243.117:9091/mobile/api",
	    "getCommentUrl": 'http://61.186.243.117:9091/itsm17/workorder/opinion/getByOrderId?orderId=',
	    "saveCommentUrl": 'http://61.186.243.117:9091/itsm17/workorder/opinion/save'
}

var Store = {

}
  Store.saveObject=function(key, object) {
    window.localStorage[key] = JSON.stringify(object);
  }

  Store.loadObject=function(key) {
    var objectString = window.localStorage[key];
    return objectString == null ? null : JSON.parse(objectString);
  }

  Store.deleteObject=function(key) {
    window.localStorage[key] = null;
  }

  Store.clear=function() {
    window.localStorage.clear();
  }
var Session={


}

  Session.saveObject=function(key, object) {
    window.sessionStorage[key] = JSON.stringify(object);
  }

  Session.loadObject=function(key) {
    var objectString = window.sessionStorage[key];
    return objectString == null ? null : JSON.parse(objectString);
  }

  Session.deleteObject=function(key) {
    window.sessionStorage[key] = null;
  }

var App = function(){
	var $ionicNavBarDelegate = null;
  var $ionicLoading = null;
  var $http         = null;
  var $state        = null;
  var $scope        = null;
  var $ionicPopup   = null;
  var $timeout      = null;

  var myNotice = function(msg, timeout, prev, post){
    $ionicLoading.show({template:msg});
    $timeout(function(){prev && prev();$ionicLoading.hide();post && post();}, timeout || 2000);
    return false;
  }

  var myRemote = function(target, params, done, fail,showloading){
    
    if(showloading){
      $ionicLoading.show({content : 'Loading',animation : 'fade-in'});
    }

    $http.post(target, params, {'timeout' : 50000}).success(function(data){
      
      $ionicLoading.hide();
      if(data.code == "200"){
        done && done(data);
      }else if (data.code == "601") {

			var alertPopup = $ionicPopup.alert({
				title : '登陆失败',
				template : '登陆失败或超时！'
			});
			alertPopup.then(function(res) {
				// 用户点击确认登录后跳转
				$state.go("login");
			});

	}else{
        
        fail ? fail(data) : myNotice(data ? data.message : '发生错误');
      }
    }).error(function(data, status){
      
        $ionicLoading.hide();
        
        fail ? fail(data) : myNotice('网络错误');

    });
  }

  var ajaxPost = function(target, params, done, fail){
    myRemote(target, params, done, fail,true);
  }
  var ajaxPost1 = function(target, params, done, fail){
    myRemote(target, params, done, fail,false);
  }

  var ajaxUpdate = function(url, data, success, error){
    $ionicLoading.show({content : 'Loading',animation : 'fade-in'});

    $.ajax({
         url: url,
         type: "POST",
         processData: false,
         contentType: false,
         data: data,
         success: function(res){
            $ionicLoading.hide();
            success(res)
         },
         error: function(){
            $ionicLoading.hide();
            error()
         }
      });
  }

  var userquery = function(){

    /*var userinfo = Session.loadObject("userinfo");
    if(userinfo == undefined || userinfo == null){
      var u = {
        "username" : Session.loadObject("user").username
      };
      var reqdata = {
        'method' : 'UserQuery',
        'param' : JSON.stringify(u)
      }

      ajaxPost(OpenAPI.requrl, $.param(reqdata), function(data){
        $scope.userinfo = data.object;
        Session.saveObject("userinfo",data.object);
      });

    }else{
      $scope.userinfo = userinfo;
    }*/


  }

  var initType = function(){

    Store.saveObject("SERVICEREQ_CATEGORY_10","桌面支持服务");
    Store.saveObject("SERVICEREQ_CATEGORY_20","会议支持服务");
    Store.saveObject("SERVICEREQ_CATEGORY_30","网络支持服务");
    Store.saveObject("SERVICEREQ_CATEGORY_40","应用系统服务");
    Store.saveObject("SERVICEREQ_CATEGORY_50","其他服务");

    Store.saveObject("EVENT_CATEGORY_10","IT终端服务");
    Store.saveObject("EVENT_CATEGORY_20","会议系统");
    Store.saveObject("EVENT_CATEGORY_30","网络");
    Store.saveObject("EVENT_CATEGORY_40","主机与中间件");
    Store.saveObject("EVENT_CATEGORY_50","机房");
    Store.saveObject("EVENT_CATEGORY_60","应用系统");

  }

  var getTypeName = function(typecode){

    var name = Store.loadObject(typecode);

    if(name == undefined || name == null){
      return "";
    }
    return name;
  }


  var setMyIonicLoading = function(obj){$ionicLoading = obj;}
  var setMyHttp         = function(obj){$http         = obj;}
  var setMyTimeout      = function(obj){$timeout      = obj;}
  var setMyIonicPopup     = function(obj){$ionicPopup      = obj;}
  var setMyState      = function(obj){$state      = obj;}
  var setScope      = function(obj){$scope      = obj;}


  var app = {}

  app.myNotice = myNotice;
  app.setMyIonicLoading      = setMyIonicLoading;
  app.setMyHttp        = setMyHttp;
  app.setMyTimeout     = setMyTimeout;

  app.setMyIonicPopup     = setMyIonicPopup;
  app.setMyState     = setMyState;
  app.setScope     = setScope;


  app.ajaxPost = ajaxPost;
  app.ajaxPost1 = ajaxPost1;
  app.ajaxUpdate = ajaxUpdate;
  app.userquery = userquery;
  app.initType = initType;
  app.getTypeName = getTypeName;

  return app;


}();





Date.prototype.Format = function(formatStr) 
{ 
var str = formatStr; 
var Week = ['日','一','二','三','四','五','六']; 
 
str=str.replace(/yyyy|YYYY/,this.getFullYear()); 
str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
  
str=str.replace(/MM/,(this.getMonth()+1)>9?(this.getMonth()+1).toString():'0' + (this.getMonth()+1));
 str=str.replace(/M/g,this.getMonth()); 
 
str=str.replace(/w|W/g,Week[this.getDay()]); 
 
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
 str=str.replace(/d|D/g,this.getDate()); 
 
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
 str=str.replace(/h|H/g,this.getHours()); 
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
 str=str.replace(/m/g,this.getMinutes()); 
 
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
 str=str.replace(/s|S/g,this.getSeconds()); 
 
return str; 
} 
function addDate(dateStr,days){
 var d=new Date(Date.parse(dateStr.replace(/-/g, "/")))
 d.setDate(d.getDate()+days);
 var m=d.getMonth()+1;
 return d.getFullYear()+'-'+(m>9?m:("0"+m))+'-'+(d.getDate()>9?d.getDate():("0"+d.getDate()));
} 
function StringToDate(DateStr) 
{ 
   
  var converted = Date.parse(DateStr); 
  var myDate = new Date(converted); 
  if (isNaN(myDate)) 
  { 
  //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-'; 
  var arys= DateStr.split('-'); 
  myDate = new Date(arys[0],--arys[1],arys[2]); 
  } 
  return myDate; 
}